package cn.com.ayqu.module.erp.dal.mysql.activityuser;

import java.util.*;

import cn.com.ayqu.framework.common.pojo.PageResult;
import cn.com.ayqu.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.com.ayqu.framework.mybatis.core.mapper.BaseMapperX;
import cn.com.ayqu.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.com.ayqu.module.erp.dal.dataobject.activityuser.ActivityUserDO;
import cn.com.ayqu.module.erp.dal.dataobject.sale.ErpCustomerCarNumberDO;
import cn.com.ayqu.module.erp.dal.dataobject.sale.ErpCustomerDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.com.ayqu.module.erp.controller.admin.activityuser.vo.*;

/**
 * 用户参与活动 Mapper
 *
 * @author 高连杰
 */
@Mapper
public interface ActivityUserMapper extends BaseMapperX<ActivityUserDO> {

    default PageResult<ActivityUserDO> selectPage(ActivityUserPageReqVO reqVO) {
        MPJLambdaWrapper<ActivityUserDO> query = new MPJLambdaWrapperX<ActivityUserDO>()
                .eq(ActivityUserDO::getActivityId, reqVO.getActivityId())
                .groupBy(ActivityUserDO::getUserId)
                .orderByDesc(ActivityUserDO::getId);

        if (StringUtils.isNotBlank(reqVO.getCustomerInfo())) {
            query.leftJoin(ErpCustomerDO.class, ErpCustomerDO::getId, ActivityUserDO::getUserId)
                    .and(q -> q.like(ErpCustomerDO::getName, reqVO.getCustomerInfo())
                            .or()
                            .like(ErpCustomerDO::getMobile, reqVO.getCustomerInfo())
                            .or()
                            .like(ErpCustomerDO::getAlias, reqVO.getCustomerInfo()))
                    .groupBy(ErpCustomerDO::getId);
        }

        if (StringUtils.isNotBlank(reqVO.getCarNumber())) {
            query.leftJoin(ErpCustomerCarNumberDO.class, ErpCustomerCarNumberDO::getCustomerId, ActivityUserDO::getUserId)
                    .like(ErpCustomerCarNumberDO::getCarNumber, reqVO.getCarNumber());
        }

        return selectJoinPage(reqVO, ActivityUserDO.class, query);
    }

}